package com.hifar.test.sensor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hifar.plat.maindatasource.service.IMainDataSourceService;
import com.hifar.plat.officUtil.ExcelTemplateUtil;
import com.hifar.sys.BusiResponse;
import com.hifar.sys.RequestConver;
import com.hifar.test.sensor.mapper.HfResSensorMapper;
import com.hifar.test.sensor.pojo.HfResSensor;
import com.hifar.test.sensor.service.IHfResSensorService;
import com.hifar.utils.ExcelImportUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 环境-试验-传感器管理 服务实现类
 * </p>
 *
 * @author wangkepeng
 * @since 2024-12-11 11:58:47
 */
@Service
public class HfResSensorServiceImpl extends ServiceImpl<HfResSensorMapper, HfResSensor> implements IHfResSensorService {

    @Autowired
    private IMainDataSourceService mainDataSourceService;

    @Override
    public BusiResponse exportExcel(Map<String, Object> pageInfo) {
        BusiResponse busiResponse = new BusiResponse();
        String ids = (String) pageInfo.get("ids");
        String operationType = (String) pageInfo.get("operationType");
        if (StringUtils.isNotBlank(operationType) && (operationType.equals("template"))) {
            ExcelTemplateUtil.exportExcelTemplate(busiResponse, HfResSensor.class, "传感器信息", "传感器信息");
            return busiResponse;
        } else {
            QueryWrapper<HfResSensor> postParam = RequestConver.getPostParam(pageInfo);
            postParam.ne("del_flag", 1);
            postParam.orderByDesc("create_time");
            if (StringUtils.isNotBlank(ids)) {
                postParam.in("id", ids);
            }
            List<HfResSensor> dataList = this.list(postParam);
            if (dataList != null && dataList.size() > 0) {
                ExcelTemplateUtil.exportExcel(busiResponse, dataList, HfResSensor.class, "传感器信息",
                        "传感器基本信息");
            } else {
                ExcelTemplateUtil.exportExcelTemplate(busiResponse, HfResSensor.class, "传感器信息", "传感器信息");
            }
            return busiResponse;
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public BusiResponse importExcel(MultipartFile file) throws Exception {
        if (file != null && !file.isEmpty()) {
            List<HfResSensor> importList = ExcelImportUtils.importExcel(file.getInputStream(),
                    HfResSensor.class);
            List<HfResSensor> insertList = new ArrayList<>();
            List<HfResSensor> updateList = new ArrayList<>();
            if (importList != null && importList.size() > 0) {
                List<String> sensorCodeList = importList.stream()
                        .map(HfResSensor::getSensorCode)
                        .collect(Collectors.toList());
                QueryWrapper<HfResSensor> sensorQueryWrapper = new QueryWrapper<>();
                sensorQueryWrapper.lambda()
                        .in(HfResSensor::getSensorCode, sensorCodeList);
                List<HfResSensor> sensorList = this.list(sensorQueryWrapper);
                Map<String, String> codeIdMap = sensorList.stream()
                        .collect(Collectors.toMap(
                                HfResSensor::getSensorCode,
                                HfResSensor::getId,
                                (o1, o2) -> o2
                        ));
                for (HfResSensor resSensor : importList) {
                    String sensorCode = resSensor.getSensorCode();
                    String id = codeIdMap.get(sensorCode);
                    if (Objects.nonNull(id)) {
                        resSensor.setStatus("1");
                        resSensor.setId(id);
                        updateList.add(resSensor);
                    } else {
                        insertList.add(resSensor);
                    }
                }
                if (!updateList.isEmpty()) {
                    this.updateBatchById(updateList);
                }
                if (!insertList.isEmpty()) {
                    this.saveBatch(insertList);
                }
                return BusiResponse.ok("导入成功！");
            }
        }
        return BusiResponse.error("导入失败，原因未知！");
    }

}
